//
//  NewFeatureCell.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/23.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit

class NewFeatureCell: UICollectionViewCell
{

    
    //从外界获取的图片资源字符
    var imgStr:String?{
        
        //类似于重写set方法
        didSet
        {
            //????为什么要再一次设置??
            setButton.hidden = true
            
            guard let img = imgStr else
            {
                return
            }
            
            //在set方法内设置背景图片
            self.cellImage.image = UIImage(named:img)
            
            
        }
    }
    
    //懒加载控件
    //设置cell的背景图片
    private lazy var cellImage:UIImageView = UIImageView()
    
    //懒加载点击button
    private lazy var setButton:UIButton = {
       
        let button = UIButton()
        
        // 添加点击事件
        button.addTarget(self, action: #selector(NewFeatureCell.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        // 设置title
        button.setTitle("欢迎光临", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.sizeToFit()
        return button
    }()
    
    //添加button的点击事件
    @objc private func buttonClick()
    {
        //跳转到欢迎界面
        UIApplication.sharedApplication().keyWindow?.rootViewController = WelocomeController()
    }
    
    //button开始显现出来的动画
    func startAnim()
    {
        self.setButton.hidden = false
        
        self.setButton.transform = CGAffineTransformMakeScale(0, 0)
        self.setButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.setButton.transform = CGAffineTransformIdentity
        }) { (_) -> Void in
            self.setButton.userInteractionEnabled = true
        }
    }
    
    //重写init方法用来设置图片
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //重新设置图片
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //重新布局界面
    func setUpUI()
    {
        //添加控件
        contentView.addSubview(self.cellImage)
        contentView.addSubview(self.setButton)
        //添加控件约束
        cellImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        setButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-100)
        }
        
        // 默认开始按钮全部隐藏
        setButton.hidden = true
    }
    
}
